package com.sicnu.carrent.controller;


//后台管理控制层

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.sicnu.carrent.domain.Brand;
import com.sicnu.carrent.domain.Cars;
import com.sicnu.carrent.domain.Users;
import com.sicnu.carrent.json.MyResponse;
import com.sicnu.carrent.serivce.BrandService;
import com.sicnu.carrent.serivce.CarsService;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("admin")
public class AdminController {

    private static final int PAGE_SIZE = 10; //规定每页显示十条数据

    @Resource
    private CarsService carsService;

    @Resource
    private BrandService brandService;

    //跳转到后台登陆成功后的首页
    @RequestMapping("toMain")
    public String toMain(){
        //List<Cars> carsList = carsService.findAllCars();
        return "admin/main";   //真正的动作地址是： http://localhost:8080/carrent/admin/toMain
    }


    //跳转到后台登陆成功后的首页
    @RequestMapping("findAllCars")
    public String findAllCars(Model model){
        List<Cars> carsList = carsService.findAllCars();
        model.addAttribute("carsList",carsList);
        return "admin/cars";   //真正的动作地址是： http://localhost:8080/carrent/admin/toMain
    }


    @RequestMapping("findPagerCars")
    public String findPagerCars(@RequestParam(name="currentPageNumber",defaultValue = "0",required = false) int currentPageNumber, Model model){

        Page<Cars> pager = carsService.findPagerCars(currentPageNumber,PAGE_SIZE);
        List<Cars> carsList = pager.getContent();
        model.addAttribute("carsList",carsList);
        model.addAttribute("pager",pager);

        return "admin/cars";   //真正的动作地址是： http://localhost:8080/carrent/admin/toMain
    }


    @RequestMapping("toAddCars")
    public String toAddCars(Model model){

        List<Brand> brandList = brandService.findAllBrand();
        model.addAttribute("brandList",brandList);
        return "admin/add_cars";
    }

    @RequestMapping("toUpdateCars")
    public String toUpdateCars(){
        return "admin/update_cars";
    }


    /*
    * @RequestMapp("toUpdateCars/{id}")
    * public String toUpdate(@PathVaribale(name="id") String id){
    *
    *   ////////
    * }
    *
    * */

    //保存汽车资料的方法
    @RequestMapping("saveCars")
    @ResponseBody
    public MyResponse saveCars(String param){
        MyResponse resp = new MyResponse();
        Cars c =  JSON.parseObject(param, new TypeReference<Cars>() {});

        try {
            c.setStatus(1); //表示汽车在售。
            carsService.save(c);
            resp.success();
        } catch (Exception e) {
            e.printStackTrace();
            resp.failure();
        }
        return resp;

    }

    //删除汽车资料的方法
    @RequestMapping("deleteCars")
    @ResponseBody

    public MyResponse deleteCars(String id){
        MyResponse resp = new MyResponse();
        //Cars c =  JSON.parseObject(param, new TypeReference<Cars>() {});

        try {
            //c.setStatus(1); //表示汽车在售。
            carsService.delete(Integer.parseInt(id));
            resp.success();
        } catch (Exception e) {
            e.printStackTrace();
            resp.failure();
        }
        return resp;
    }
}
